//
//  SystemSettingInfoVC.m
//  Brand
//
//  Created by Faker on 5/16/14.
//  Copyright (c) 2014 Faker. All rights reserved.
//

#import "SystemSettingInfoVC.h"
#import "Header.h"
#import "config.h"

#define FeedBackDefaultText             @"写下您的意见或建议吧"
#define ContactDefaultText              @"请输入您的邮箱或QQ号（可选)"
#define DefaultColor                    RGB(204,204,204)
#define DefaultHeight                   (isiOS6?kScreen_Height-self.navTopContentView.bottom-40:kScreen_Height-self.navTopContentView.bottom-20)

@interface SystemSettingInfoVC ()
@property (weak, nonatomic) IBOutlet UIView *navTopContentView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (assign, nonatomic) float defaultTop;

//意见反馈
@property (weak, nonatomic) IBOutlet UIView *feedBackView;
@property (weak, nonatomic) IBOutlet UIView *textBgView1;
@property (weak, nonatomic) IBOutlet UITextView *textView1;
@property (weak, nonatomic) IBOutlet UIView *textBgView2;
@property (weak, nonatomic) IBOutlet UITextView *textView2;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;

//隐私政策
@property (strong, nonatomic) IBOutlet UIView *pravicyView;
@property (weak, nonatomic) IBOutlet UIWebView *myWebView;


//version
@property (weak, nonatomic) IBOutlet UIView *versionView;
@property (weak, nonatomic) IBOutlet UIImageView *logoImageView;
@property (weak, nonatomic) IBOutlet UILabel *versionLabel;
@property (weak, nonatomic) IBOutlet UILabel *copyrightLabel;
@property (weak, nonatomic) IBOutlet UIView *infoBgView;



@end

@implementation SystemSettingInfoVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self drawNavTopContentView:self.navTopContentView];
    [self setUp];
}

-(void)setUp
{
    self.titleLabel.text = self.title;
    self.defaultTop = self.bgView.top;
    switch (self.tag) {
        case 1:
            [self initpravicy];
            break;
        case 2:
            [self initFeedBackView];
            break;
        case 3:
            [self initVersionInfo];
            break;
        default:
            break;
    }
}

#pragma mark - 意见反馈
-(void)initFeedBackView
{
    [[[NSBundle mainBundle] loadNibNamed:@"FeedBackCell" owner:self options:nil]lastObject];
    [self.bgView addSubview:self.feedBackView];
    self.textBgView1.layer.cornerRadius = 6;
    self.textView1.layer.cornerRadius = 5;
    self.textBgView2.layer.cornerRadius = 6;
    self.textView2.layer.cornerRadius = 5;
    
    self.textView1.text = FeedBackDefaultText;
    self.textView1.textColor = DefaultColor;
    self.textView2.text = ContactDefaultText;
    self.textView2.textColor = DefaultColor;
    self.submitBtn.enabled = NO;
    
    UITapGestureRecognizer *tapGes=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardWillHide)];
    [self.bgView addGestureRecognizer:tapGes];
    
    [self.view bringSubviewToFront:self.bgView];
    
}

#pragma mark - 隐私政策
-(void)initpravicy
{
    [[NSBundle mainBundle] loadNibNamed:@"pravicyCell" owner:self options:nil];
    self.pravicyView.height = self.bgView.height;
    [self.bgView addSubview:self.pravicyView];
    [self prepareGetPrivacyData];
}


-(void)prepareGetPrivacyData
{
    NSString* url = [NSString stringWithFormat:@"%@getUserAgreement",API_BASE];
    [[AFGetData sharedInstance] startGet:url andResultIsJSON:YES andRequestCompletion:^(id responseObj, BOOL isSuccess) {
        if(isSuccess)
        {
            [self getPravicyDataSuccess:responseObj];
        }
        else{
            [self getPravicyDataError];
        }
    }];
}

-(void)getPravicyDataSuccess:(id)responseObj
{
    NSDictionary* dic = (NSDictionary*)responseObj;
    NSString* url = [dic objectForKey:@"url"];
    
    NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    self.myWebView.delegate = self;
    [self.myWebView loadRequest:request];

}



-(void)getPravicyDataError
{}

#pragma mark - webViewDelegate  methods
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [kApp hideWaitView];
    [PopTips tips:self.view Msg:NotifyNoNet];
    [self performSelector:@selector(goBack:) withObject:self afterDelay:0.7];
}

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [kApp displayWaitViewWithAllMaskView:NO];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [kApp hideWaitView];
}


#pragma mark - 版本信息
-(void)initVersionInfo
{
    [[NSBundle mainBundle] loadNibNamed:@"VersionInfoCell" owner:self options:nil];
    self.logoImageView.layer.cornerRadius = 10;
    self.logoImageView.clipsToBounds = YES;
    
    self.versionLabel.text = [NSString stringWithFormat:@"version for iPhone %@",APPVersion];
    
    
    
    [self.bgView addSubview:self.versionView];
}


#pragma mark - 键盘事件
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    if(textView == self.textView1)
    {
        if([textView.text isEqualToString:FeedBackDefaultText]){
            textView.text = @"";
        }
        [UIView animateWithDuration:0.3 animations:^{
            self.bgView.top=self.defaultTop-45;
        }];
    }
    else{
    
        if([textView.text isEqualToString:ContactDefaultText]){
            textView.text = @"";
        }
        [UIView animateWithDuration:0.3 animations:^{
            self.bgView.top=self.defaultTop-45;
        }];
    }
    textView.textColor = RGB(53, 53, 53);
}

-(void)textViewDidChange:(UITextView *)textView
{
    if (textView == self.textView1 ) {
        if ([Common removeCharacterSpaceAndEnter:textView.text].length > 0) {
            self.submitBtn.enabled=YES;
        }else{
            self.submitBtn.enabled=NO;
        }
    }

}

- (void)keyboardWillHide{
    [self.textView1 resignFirstResponder];
    [self.textView2 resignFirstResponder];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.bgView.top=self.defaultTop;
        
    }completion:^(BOOL finished) {
        if([self.textView1.text isEqualToString:@""]){
            self.textView1.text = FeedBackDefaultText;
            self.textView1.textColor = DefaultColor;
        }
        if([self.textView2.text isEqualToString:@""]){
            self.textView2.text = ContactDefaultText;
            self.textView2.textColor = DefaultColor;
        }
    }];
    
}


#pragma mark - xib点击事件
- (IBAction)goBack:(id)sender {
    
    [kApp hideWaitView];
    [[AFGetData sharedInstance] removeAllGetAndPostRequest];
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)commit:(id)sender {
    
    NSString* url = [NSString stringWithFormat:@"%@feedback",API_BASE];
    NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:self.textView1.text,@"cont",self.textView2.text,@"contact",@"2",@"pf",APPVersion,@"app_version",@"苹果",@"phone_version",[Common getOSVersion],@"system_version", nil];
    [[AFGetData sharedInstance] startPost:url andInfo:dic andDataBodyBlock:^(id<AFMultipartFormData> formData) {
        
    } andRequestCompletion:^(id responseObj, BOOL isSuccess) {
        if(isSuccess)
        {
            [self commitSuccess:responseObj];
        }
        else{
            [self commitError];
        }
    }];
}

-(void)commitSuccess:(id)respinseObj
{
    [PopTips tipsAtTop:self.view Msg:@"提交成功"];
    [self keyboardWillHide];
    
}

-(void)commitError
{
    [PopTips tipsAtTop:self.view Msg:@"提交失败"];
    [self keyboardWillHide];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
